<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\captcha\Captcha;
use think\facade\Session;
use think\facade\Config;
use think\Db;

class Sitemap extends controller
{   
	/*分类页生成*/
    public function index(){
   	    $filename='sitemap_dan.xml';
   	    $strtxt='';
   	    $strxml="<?xml version='1.0' encoding='UTF-8'?>\n<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:xhtml='http://www.w3.org/1999/xhtml' xmlns:image='http://www.google.com/schemas/sitemap-image/1.1'>\n";
        $class_data=Db::table('lily_classify')->field('id,route_id,iszhong')->where([['status','=',1],['route_id','<>',0],['is_tag','<>',1],['is_sitemap','=',1]])->select();
		foreach ($class_data as $key => $value) {
			$total=class_uplower($value['id']);
			$class_url='';
			for($i=count($total)-1; $i>=0 ; $i--) {
			    if($total[$i]['is_tag']!='1'){
			        if(!empty($total[$i]['route_name'])){
			            $class_url.=$total[$i]['route_name'].'/';
			        }else{
			            $class_url.=change_lower_symbol($total[$i]['cname']).'/';
			        }
			    }
			}
			$strxml.="\n<url>\n";
            $strxml.="<loc><![CDATA[https://".Config::get('app.web_host_name')."/".$class_url."]]></loc>\n";
            $strxml.="<xhtml:link rel='alternate' hreflang='en' href='https://".Config::get('app.web_host_name')."/".$class_url."'/>\n";
            if($value['iszhong']!=1){
                $strxml.="<xhtml:link rel='alternate' hreflang='zh' href='https://".str_replace('lily-bearing','lilybearing',Config::get('app.web_host_name'))."/".$class_url."'/>\n";
            }
			$strxml.="<lastmod>".date('Y-m-d',time())."</lastmod>\n</url>";
			$strtxt.='https://'.Config::get('app.web_host_name')."/".$class_url."/\n";
		}
		$strxml.="\n</urlset>";
		$load=explode('application', __DIR__);
		if(file_exists($load[0].'public/'.$filename)){
	        unlink($load[0].'public/'.$filename);
	    }
	    $myfiletxt=fopen($load[0].'public/'.$filename, "a+");
	    fwrite($myfiletxt, $strxml);
	    fclose($myfiletxt);
	    $myfiletxt=fopen($load[0].'public/sitemap.txt', "a+");
        fwrite($myfiletxt, $strtxt);
        fclose($myfiletxt);
    }

    public function blog(){
        $filename='sitemap_blog.xml';
        $strtxt='';
        $strxml="<?xml version='1.0' encoding='UTF-8'?>\n<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:xhtml='http://www.w3.org/1999/xhtml' xmlns:image='http://www.google.com/schemas/sitemap-image/1.1'>\n";
        $blog=Db::connect('publicdata')->table('lily_blog')->field('url')->where("status=1")->select();
        foreach ($blog as $key => $value) {
            $strxml.="\n<url>\n";
            $strxml.="<loc><![CDATA[https://".Config::get('app.web_host_name')."/resources/blog/".$value['url']."/]]></loc>\n";
            $strxml.="<xhtml:link rel='alternate' hreflang='en' href='https://".Config::get('app.web_host_name')."/resources/blog/".$value['url']."/'/>\n";
            /*$strxml.="<xhtml:link rel='alternate' hreflang='zh' href='https://".str_replace('lily-bearing','lilybearing',Config::get('app.web_host_name')).'/resources/blog/'.$value['url']."/'/>\n";*/
            $strxml.="<lastmod>".date('Y-m-d',time())."</lastmod>\n</url>";
            $strtxt.='https://'.Config::get('app.web_host_name')."/resources/blog/".$value['url']."/\n";
        }
        $strxml.="\n</urlset>";
        $load=explode('application', __DIR__);
		if(file_exists($load[0].'public/'.$filename)){
	        unlink($load[0].'public/'.$filename);
	    }
	    $myfiletxt=fopen($load[0].'public/'.$filename, "a+");
	    fwrite($myfiletxt, $strxml);
	    fclose($myfiletxt);

	    $myfiletxt=fopen($load[0].'public/sitemap.txt', "a+");
        fwrite($myfiletxt, $strtxt);
        fclose($myfiletxt);
    }

    public function events(){
        $filename='sitemap_events.xml';
        $strtxt='';
        $strxml="<?xml version='1.0' encoding='UTF-8'?>\n<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:xhtml='http://www.w3.org/1999/xhtml' xmlns:image='http://www.google.com/schemas/sitemap-image/1.1'>\n";
        $blog=Db::connect('publicdata')->table('lily_events')->field('url')->where("status=1")->select();
        foreach ($blog as $key => $value) {
            $strxml.="\n<url>\n";
            $strxml.="<loc><![CDATA[https://".Config::get('app.web_host_name')."/resources/news-and-events/".$value['url']."/]]></loc>\n";
            $strxml.="<xhtml:link rel='alternate' hreflang='en' href='https://".Config::get('app.web_host_name')."/resources/news-and-events/".$value['url']."/'/>\n";
            /*$strxml.="<xhtml:link rel='alternate' hreflang='zh' href='https://".str_replace('lily-bearing','lilybearing',Config::get('app.web_host_name')).'/resources/news-and-events/'.$value['url']."/'/>\n";*/
            $strxml.="<lastmod>".date('Y-m-d',time())."</lastmod>\n</url>";
            $strtxt.='https://'.Config::get('app.web_host_name')."/resources/news-and-events/".$value['url']."/\n";
        }
        $strxml.="\n</urlset>";
        $load=explode('application', __DIR__);
		if(file_exists($load[0].'public/'.$filename)){
	        unlink($load[0].'public/'.$filename);
	    }
	    $myfiletxt=fopen($load[0].'public/'.$filename, "a+");
	    fwrite($myfiletxt, $strxml);
	    fclose($myfiletxt);

	    $myfiletxt=fopen($load[0].'public/sitemap.txt', "a+");
        fwrite($myfiletxt, $strtxt);
        fclose($myfiletxt);
    }

    public function product_page(){
        ini_set('max_execution_time', '0');
        $filename='sitemap';
        $count=Db::table('lily_products')->where('url_product','not like','%\_%')->count();
        
        $limit=20000;
        $p=ceil($count/$limit);
        for($i=1;$i<=$p;$i++){
            if($i!=1){
            	$load=explode('application', __DIR__);
				if(file_exists($load[0].'public/'.$filename.$i.'.xml')){
			        unlink($load[0].'public/'.$filename.$i.'.xml');
			    }
			    $myfiletxt=fopen($load[0].'public/'.$filename.$i.'.xml', "a+");
			    $str="";
			    fwrite($myfiletxt, $str);
			    fclose($myfiletxt);
                
                $s=($i-1)*$limit;
                $this->produce_sitemap_product($s,$limit,$i);
            }else{
                $this->produce_sitemap_product(0,$limit,'');
            }
        }

    }


    public function produce_sitemap_product($s,$end,$i){
        ini_set('max_execution_time', '0');
        $filename='sitemap';
        $product=Db::table('lily_products')->field('url_product,img,class_id')->where('url_product','not like','%\_%')->limit($s,$end)->select();
        $strxml="<?xml version='1.0' encoding='UTF-8'?>\n<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:xhtml='http://www.w3.org/1999/xhtml' xmlns:image='http://www.google.com/schemas/sitemap-image/1.1'>\n";
        $strtxt="";
        foreach ($product as $key => $value) {
            $iszhong=Db::table('lily_classify')->field('iszhong')->where('id',$value['class_id'])->find()['iszhong'];
            $strxml.="\n<url>\n";
            $strxml.="<loc><![CDATA[https://".Config::get('app.web_host_name')."/products/".$value['url_product']."/]]></loc>\n";

            $strxml.="<xhtml:link rel='alternate' hreflang='en' href='https://".Config::get('app.web_host_name').'/products/'.$value['url_product']."/'/>\n";
            if($iszhong!=1){
                $strxml.="<xhtml:link rel='alternate' hreflang='zh' href='https://".str_replace('lily-bearing','lilybearing',Config::get('app.web_host_name')).'/products/'.$value['url_product']."/'/>\n";
            }
            if(!empty($value['img'])){
                $strxml.="<image:image><image:loc>".config('app.images_host').$value['img']."</image:loc></image:image>\n";
            }
            $strxml.="<lastmod>".date('Y-m-d',time())."</lastmod>\n</url>";
            $strtxt.='https://'.Config::get('app.web_host_name')."/products/".$value['url_product']."/\n";
        }
        $strxml.="\n</urlset>";

        $load=explode('application', __DIR__);
		if(file_exists($load[0].'public/'.$filename.$i.'.xml')){
	        unlink($load[0].'public/'.$filename.$i.'.xml');
	    }
	    $myfiletxt=fopen($load[0].'public/'.$filename.$i.'.xml', "a+");
	    fwrite($myfiletxt, $strxml);
	    fclose($myfiletxt);

	    $myfiletxt=fopen($load[0].'public/sitemap.txt', "a+");
        fwrite($myfiletxt, $strtxt);
        fclose($myfiletxt);
    }



}